library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Un shapefile est un fichier contenant toute l’information liée à la géométrie des objets qu’il contient. Le format de fichier associé (.shp) est devenu un standard parmi tous les outils de SIG (Systèmes d’Informations Géographiques) et est notamment exploitable sous R. Attention, le fichier .shp est accompagné d’une série d’autres fichiers portant le même nom (.dbf, .xml, .shx, …) qui doivent être conservés au même endroit que le shapefile.
Le logiciel libre QGIS permet de créer/manipuler/éditer
des shapefiles rapidement pour ensuite les exporter vers l’environnement
R et générer des cartes. Si le logiciel est installé sur votre machine,
ouvrez data/Loire_GIS.qgz.
Une fois le shapefile généré, il est nécessaire de le charger dans
l’environnement R. Le package permettant la manipulation de ces fichiers
est sf1.
Loire_coast <- sf::st_read('data/Loire_shapefile/coastline.shp', crs = 4326)
## Reading layer `coastline' from data source
## `C:\Users\rlecuyer\Desktop\Aide_thèse\Mickey_carto\data\Loire_shapefile\coastline.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -2.503909 ymin: 47.10874 xmax: -1.754918 ymax: 47.31379
## Geodetic CRS: WGS 84
Les fonctionnalités de R base permettent de visualiser
rapidement ce à quoi ressemblent les objets contenus dans ce
shapefile.
plot(Loire_coast$geometry)
R baseÀ partir de ces connaissances, il est possible de générer une représentation cartographique simple.
Loire_tidal_level <- sf::st_read('data/Loire_shapefile/tidal_level.shp', crs = 4326)
## Reading layer `tidal_level' from data source
## `C:\Users\rlecuyer\Desktop\Aide_thèse\Mickey_carto\data\Loire_shapefile\tidal_level.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 3 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -2.503909 ymin: 47.10874 xmax: -1.754918 ymax: 47.31379
## Geodetic CRS: WGS 84
tidal_level_color <- c('#DDDDCC', '#AACCFF', '#AAFFCC')
plot(Loire_coast$geometry, lwd = 2,
main = "Les surfaces marnantes de l'estuaire de la Loire")
plot(Loire_tidal_level['level'],
border = NA,
pal = tidal_level_color,
add = TRUE)
Pour la cartographie sous R sans utilisation de packages, ça s’arrête plus ou moins là. Une représentation cartographique plus travaillée pourrait inclure différentes informations complémentaires : flèche du nord, échelle …
ggplot2Prenons ce magnifique classement des 10 plus beaux coins de pêche en France2.
France_fond <- sf::read_sf('data/Peche_fond.shp')
spot_peche <- read.csv2('data/Peche_meilleurcoin.csv') %>%
mutate(classement = as.numeric(rownames(.)))
head(spot_peche)
## lieu latitude longitude classement
## 1 La Sioule (03) 46.31218 3.297298 1
## 2 La Beaume (07) 44.48011 4.253149 2
## 3 Le Lac de l Abbaye (39) 46.52948 5.910707 3
## 4 Le Cousin (89) 47.48173 3.908853 4
## 5 Le Lac du Bourget (73) 45.73091 5.860195 5
## 6 Quiberon (56) 47.47771 -3.119289 6
plotlyUne carte dynamique permet un minimum d’interaction avec la carte. Nous pouvons enfin aller voir où se trouve ce super spot de pêche sur l’île de la Réunion.
map_peche <- map_peche + geom_text(aes(label = lieu), size = 2.5, nudge_y = -.3)
plotly::ggplotly(map_peche)
leaflet et les cartes
GooglemapStates = maps::map("world", fill = TRUE, plot = FALSE)
leaflet::leaflet(data = spot_peche) %>%
leaflet::addTiles(urlTemplate = "https://mts1.google.com/vt/lyrs=s&hl=en&src=app&x={x}&y={y}&z={z}&s=G", attribution = 'Google') %>%
leaflet::setView(-1,47,5) %>%
leaflet::addCircleMarkers(popup = ~lieu)
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
ShinyConnexion à une base de données géoréférencées via
RPostgreSQL.
##
## Attachement du package : 'DT'
## Les objets suivants sont masqués depuis 'package:shiny':
##
## dataTableOutput, renderDataTable
##
## Attachement du package : 'plotly'
## L'objet suivant est masqué depuis 'package:ggplot2':
##
## last_plot
## L'objet suivant est masqué depuis 'package:stats':
##
## filter
## L'objet suivant est masqué depuis 'package:graphics':
##
## layout
##
## Attachement du package : 'kableExtra'
## L'objet suivant est masqué depuis 'package:dplyr':
##
## group_rows
## Le chargement a nécessité le package : DBI
## This is 'rpostgis' version 1.4.4 (2023-05-06).
##
## Due to retirement of the package 'rgeos', 'rpostgis' will retire in September 2023.
## * For vector operations, please check package 'sf', which provides a mechanism to connect to PostGIS databases.
## * For raster operations, no alternative solution is identified yet.
## * For general database operations, use 'RPostgreSQL' directly.
##
## If you are interested in the development and maintenance of 'rpostgis', please check:
## https://github.com/mablab/rpostgis/issues/28
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
## Le chargement a nécessité le package : sp
## Please note that rgdal will be retired during 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-6, (SVN revision 1201)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.2, released 2022/09/02
## Path to GDAL shared files: C:/Users/rlecuyer/AppData/Local/Programs/R/R-4.2.3/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.2\proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## `summarise()` has grouped output by 'cell_no'. You can override using the
## `.groups` argument.
ui <- fluidPage(
titlePanel("Exploration de données par eco_type"),
sidebarLayout(
sidebarPanel(
strong("Ma belle barre latérale"),
selectInput("select", label = h3("Choisir une annee"),
choices=ordered(unique(exec$thon_over_temp$year)), multiple=F),
)
,
mainPanel(
"Panneau principal: ici sont représentées les sorties désirées",
plotOutput("event_number"),
)
)
)
# map_TYPE$nb_event<-as.numeric(map_TYPE$nb_event)
server<- function(input, output) {
output$event_number = renderPlot({
map_TYPE_year<- exec$thon_over_temp %>%
filter(year==input$select)
ggplot(data=map_TYPE_year) +
geom_sf(aes(fill=mhw_day),color=NA, size=0.01)+
geom_sf(data = exec$geo_borders)+
coord_sf(xlim = c(-100,30),ylim = c(-40,40))+
viridis::scale_fill_viridis(name="Number of mhw day\nwith tmax=29.5°C",
option = 'turbo', trans = 'log', breaks = c(10, 20, 40, 80))+
theme(panel.grid.major.x = element_line(color = NA),panel.background = element_blank(),
axis.text = element_blank(),axis.ticks = element_blank(), axis.title = element_blank(),
plot.margin=unit(c(0,0,0,0), "cm"),
plot.title = element_text(size = 15, face = "bold", hjust = 0.5, vjust=-0.5),
legend.title = element_text(size = 15, face = "bold", hjust = 0.5, vjust=0.5),
legend.text = element_text(size=15))
})
}
shinyApp(ui, server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Un exemple d’application Shiny au format HTML (associé au script
script/Alose_app_shiny.R) :
Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10 (1), 439-446, https://doi.org/10.32614/RJ-2018-009↩︎
source des données : https://petitsfrenchies.com/les-10-plus-beaux-endroits-pour-pecher-en-france/↩︎
Comment choisir notre prochaine destination pêche ?
Il suffit de représenter la localisation de ces dix meilleurs coins de pêche sur un fond de carte de la France métropolitaine, puis de choisir le lieu de nos prochaines vacances !